package agent;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import javax.ejb.*;
import projet.Projet;
import projet.Tache;

/**
 * EJB Stateless de gestion des agents
 * @author Geoffroy
 *
 */
@Stateless
@Local
public class AgentEJB implements AgentInterface{
	
	@PersistenceContext
	EntityManager em;
	
	
	public void addAgent(Agent a) {
		em.persist(a);
	}
	
	public void update(Agent a) {
		em.merge(a);
	}
	
	public Agent findById(int id) {
		return (Agent) em.find(Agent.class,id);
	}
	
	public Collection<Tache> findTacheRetard(int idAgent) {
		//TODO
		return new ArrayList<Tache>();
	}
	/**
	 * Suppresion d'agent
	 */
	public void delete(int id) {
		em.remove(this.findById(id));
	}
	
	/**
	 *  
	 * @return la liste de tous les agents
	 */
	public ArrayList<Agent> findAll() {
		Query q = em.createQuery("SELECT a FROM Agent a");
		return (ArrayList<Agent>)q.getResultList();
	}
	
	public Agent findByUserId(int uid) {
		Query q = em.createQuery("SELECT a FROM Agent a WHERE a.utilisateur.id="+uid);
		return (Agent)q.getSingleResult();
	}
}
